package sis.studentinfo;

import java.util.*;

public class Student {
	public enum Grade {
		A(4),
		B(3),
		C(2),
		D(1),
		F(0);
		
		private int points;
		
		Grade(int points) {
			this.points = points;
		}
		
		int getPoints() {
			return points;
		}
	}
	static final int CREDITS_REQUIRED_FOR_FULL_TIME = 12;
	static final String IN_STATE = "CO";
	private String name;
	private String firstName = "";
	private String middleName = "";
	private String lastName = "";
	private String state = "";
	private int credits;
	private ArrayList<Grade> grades = new ArrayList<Grade>();
	
	private GradingStrategy gradingStrategy = 
		new BasicGradingStrategy();
		//new RegularGradingStrategy();
	
	public Student (String fullName){
		this.name = fullName;
		credits = 0;
		List<String> nameParts = split(fullName);
		setName(nameParts);
	}

	private List<String> split(String fullName) {
		List<String> results = new ArrayList<String>();
		for (String name: fullName.split(" "))
			results.add(name);
		return results;
		/*
		List<String> results = new ArrayList<String>();
		
		StringTokenizer tokenizer = new StringTokenizer(name, " ");
		while (tokenizer.hasMoreTokens())
			results.add(tokenizer.nextToken());
		return results;
		*/
		
		/*
		StringBuffer word = new StringBuffer();
		for (int index = 0; index < name.length(); index++) {
			char ch = name.charAt(index);
			if(!Character.isWhitespace(ch))
				word.append(ch);
			else
				if(word.length() > 0) {
					results.add(word.toString());
					word = new StringBuffer();
				}
		}
		if(word.length() > 0)
			results.add(word.toString());
		*/
	}
	
	private void setName(List<String> nameParts) {
		this.lastName = removeLast(nameParts);
		String name = removeLast(nameParts);
		if (nameParts.isEmpty())
			this.firstName = name;
		else {
			this.middleName = name;
			this.firstName = removeLast(nameParts);
		}
	}
	
	private String removeLast (List<String> list) {
		if (list.isEmpty())
			return "";
		return list.remove(list.size() - 1);
	}
	
	/*
		if (nameParts.size() == 1)
			this.lastName = nameParts.get(0);
		else if (nameParts.size() == 2) {
			this.firstName = nameParts.get(0);
			this.lastName = nameParts.get(1);
		}		
		else if (nameParts.size() == 3) {
			this.firstName = nameParts.get(0);
			this.middleName = nameParts.get(1);
			this.lastName = nameParts.get(2);
		}
	}
	*/

	public String getName() {
		return name;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getMiddleName() {
		return middleName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	boolean isFullTime() {
		return credits >= CREDITS_REQUIRED_FOR_FULL_TIME;
	}
	
	int getCredits() {
		return credits;
	}
	
	void addGrade(Grade grade) {
		grades.add(grade);
	}
	
	void addCredits(int credits) {
		this.credits += credits;
	}
	
	void setState(String state) {
		this.state = state;
	}
	
	void setGradingStrategy(GradingStrategy gradingStrategy) {
		this.gradingStrategy = gradingStrategy;
	}
	
	boolean isInState() {
		return state.equals(Student.IN_STATE);
	}
	
	double getGpa() {
		if(grades.isEmpty())
			return 0.0;
		double total = 0.0;
		for (Grade grade: grades) 
			total += gradingStrategy.getGradePointsFor(grade);
			//total += gradePointsFor(grade);
		return total / grades.size();
	}
	
	int gradePointsFor(Grade grade) {
		return gradingStrategy.getGradePointsFor(grade);
	}
}
